<template>
   <div class="sub-filter" v-if="filterData && !lodingFilter">
     <div class="item">
       <div class="head">品牌：</div>
       <div class="body">
         <a @click="changeBrand(item)" :class="{active:item.id === filterData.selectedBrand}" href="javascript:;" v-for="item in filterData.brands" :key="item.id">{{item.name}}</a>
       </div>
     </div>
     <div class="item" v-for="item in filterData.saleProperties" :key="item.id">
       <div class="head">{{item.name}}：</div>
       <div class="body">
         <a @click="changeProp(item,sub.id)" :class="{active:sub.id === item.selectedAttr}" href="javascript:;" v-for="sub in item.properties" :key="sub.id">{{sub.name}}</a>
       </div>
     </div>
   </div>
   <div v-else class="sub-filter">
      <XtxSkeleton class="item" width="800px" height="40px"/>
      <XtxSkeleton class="item" width="800px" height="40px"/>
      <XtxSkeleton class="item" width="600px" height="40px"/>
      <XtxSkeleton class="item" width="600px" height="40px"/>
      <XtxSkeleton class="item" width="600px" height="40px"/>
   </div>
</template>
<script>
import { ref, watch } from 'vue-demi'
import { useRoute } from 'vue-router'
import { findSubCategoryFilter } from '@/api/category'
export default {
  name: 'SubFilter',
  setup (props, { emit }) {
    // 监听二级类目的变化 获取筛选数据
    const filterData = ref(null)
    const route = useRoute()
    const lodingFilter = ref(false)
    watch(() => route.params.id, newVal => {
      lodingFilter.value = true
      // 变化后的id有值且处于二级类目
      if (newVal && `/category/sub/${newVal}` === route.path) {
        //  发请求拿数据
        findSubCategoryFilter(route.params.id).then(res => {
          // 每一组可选的筛选条件缺失
          // 给每一组数据加上选中的ID
          // 1.品牌
          res.result.selectedBrand = null
          res.result.brands.unshift({ id: null, name: '全部' })
          res.result.saleProperties.forEach(item => {
            item.selectedAttr = null
            item.properties.unshift({ id: null, name: '全部' })
          })
          // 设置修改的数据
          filterData.value = res.result
          lodingFilter.value = false
        })
      }
    }, { immediate: true })
    // 获取筛选参数的函数
    const getFilterParams = () => {
      // { brandId: null, attrs: [{ groupName: '', propertyName: '' }] }
      const obj = { brandId: null, attrs: [] }
      obj.brandId = filterData.value.selectedBrand
      filterData.value.saleProperties.forEach(item => {
        if (item.selectedAttr) {
          const prop = item.properties.find(sub => item.selectedAttr === sub.id)
          obj.attrs.push({ groupName: item.name, propertyName: prop.name })
        }
      })
      if (!obj.attrs.length) obj.attrs = null
      return obj
    }
    // 记录当前选择的品牌
    const changeBrand = (item) => {
      if (filterData.value.selectedBrand === item.id) return
      filterData.value.selectedBrand = item.id
      emit('filterChange', getFilterParams())
    }
    // 记录选择的销售属性
    const changeProp = (item, sub) => {
      if (item.selectedAttr === sub) return
      item.selectedAttr = sub
      emit('filterChange', getFilterParams())
    }
    return { filterData, lodingFilter, changeBrand, changeProp }
  }
}
</script>
<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
    .xtx-skeleton {
        padding: 10px 0;
    }
  }
</style>
